package bank;

public class Account implements ITransactionable {

	private String owner;
	private double balance;
	
	public Account(String owner) {
		this.owner = owner;
		this.balance = 0;
	}
	
	public Account(String owner, double startingBalance) {
		this.owner = owner;
		this.balance = startingBalance;
	}
	
	@Override
	public synchronized void transfer(Transaction payload) {
		// if we are on the sender side of the transaction, silently give away your money, else cash money
		if ((ITransactionable) this == payload.sender) {
			balance -= payload.amount;
			return;
		} else {
			balance += payload.amount;
		}
		// print a nice status message (only on recipient side to avoid duplicate status messages
		System.out.println(String.format("%s -> %s %f$ (recipient now has %f$)", payload.sender.getOwner(), payload.recipient.getOwner(), payload.amount, payload.recipient.getBalance()));
	}
	
	@Override
	public String getOwner() {
		return this.owner;
	}
	
	@Override
	public double getBalance() {
		return this.balance;
	}

}